<?php

class Admin_OptionsController extends Zend_Controller_Action {

    public function init() {
        if (!Zend_Auth::getInstance()->hasIdentity()) {
            $this->_redirect("/admin/login");
        }
        $this->_helper->layout->setLayout("admin");
    }

    public function indexAction() {
        
    }

    public function sendgridAction() {
        $optionsDb = new Admin_Model_Options();
        $sendgridDetails = $optionsDb->getSendgridDetails();
        $form = new Admin_Form_Sendgrid();
        $form->serverPath->setValue($sendgridDetails['serverPath']);
        $form->username->setValue($sendgridDetails['username']);
        $form->password->setValue($sendgridDetails['password']);
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                $optionsDb->updateSengridDetails(array(
                    'details' => json_encode($values),
                ));
                $this->view->done = true;
            }
        }
    }

    public function adminemailAction() {
        $optionsDb = new Admin_Model_Options();
        $form = new Admin_Form_Adminemail();
        $form->email->setValue($optionsDb->getAdminEmail());
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                $optionsDb->updateAdminEmail(array(
                    'details' => $values['email']
                ));
                $this->view->done = true;
            }
        }
    }

    public function fullonsmsAction() {
        $optionsDb = new Admin_Model_Options();
        $fullOnSmsDetails = $optionsDb->getFullOnSmsDetails();
        $form = new Admin_Form_Fullonsms();
        $form->username->setValue($fullOnSmsDetails['username']);
        $form->password->setValue($fullOnSmsDetails['password']);
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                $optionsDb->updateFullOnSms(array(
                    'details' => json_encode($values)
                ));
                $this->view->done = true;
            }
        }
    }

}

